home *** CD-ROM | disk | FTP | other *** search
- Path: amaryllisp1.appsig.com!user
- From: larry_kearney@appsig.com (Larry Kearney)
- Newsgroups: comp.lang.c
- Subject: Re: Handling complex numbers...
- Date: Fri, 08 Mar 1996 07:47:22 -0700
- Organization: Who ever said I was organized?
- Distribution: world
- Message-ID: <larry_kearney-0803960747220001@amaryllisp1.appsig.com>
- References: <4hi113$2i8k@mercury.cc.uottawa.ca>
- NNTP-Posting-Host: amaryllisp1.appsig.com
-
- > Dear fellow netters,
- >
- > Can someone kindly explain to me how to represent complex numbers in C?
- > I am writing a numerical method program to calculate the area under
- > a curve using Simpson's Rule. The problem that I face right now is
- > the representation of "i" (where i^2 = -1) in my C program.
- >
- > Here's a simple example of what I mean:
- >
- > _b
- > |
- > | exp(ix) dx
- > _|
- > a
- >
- >
- > How do I implement this function exp(ix)?
- >
- > I'm grateful for your help.
- >
- > Charles Tran
- > --
- > Charles
-
- The C language is not terribly efficient when it comes to complex numbers.
- You can represent a complex number as a struct, i.e.,
-
- typedef struct
- {
- double re; /* real part of the complex number */
- double im; /* imaginary part of the complex number */
- } complex;
-
- but the big problem is that the arithmetic operators don't know what to do
- when presented with values that are structures. As a result, the
- programmer is required to implement functions that perform that standard
- operations and that take complex structs as arguments. For example,
-
- void AddComplex ( complex *a, complex *b, complex *c )
- {
- c->real = a->real + b->real;
- c->imag = a->imag + b->imag;
- }
-
- This makes coding an expressions such as
-
- x = 2.0 * ( y + z ) / d /* all variables are complex */
-
- an exercise is calling functions and the creation of lots of temporary
- complex arguments.
-
- I'm sorry I can't offer you any more guidance about an more acceptable solution
- using the C language other that to suggest you surf the net looking for
- library packages that already implement complex arithmetic so that you
- don't have to write them yourself (but you better test them first).
- Alternate
- solutions would be to attempt to code your function using either Fortran, where
- complex arithmetic is built-in (does anybody know that anymore) or code it
- in C++ where you can easily create a complex class and then write function
- that overrides the normal arithmetic operators and trig functions when
- complex arithmetic is used. This approach would allow a more natural
- implementation of expressions.
-
- Good luck.
-
- --
- Larry Kearney | "You want fries with that?"
- Applied Signal Technology |
- larry_kearney@appsig.com |
-